import("io", true)
import("CArray")
import("CMatrix")
import("MPI")

rank = MPI.Rank();
size = MPI.Size();

function custom(din, dout)
{
    for(i=0; i<din.length; i++)
    {
      for(j=0; j<din[i].length; j++ )
      {
        if(dout[i][j] != "-")
          dout[i][j] = din[i][j]
        else
          dout[i][j] = "-"
      }
    }
        
}

/* Single value reduction
if (rank == 0)
    print ("Single value");
result = MPI.Reduce(rank+1, MPI.Prod, 0);
if (rank == 0)
    print (result, "\n");

MPI.Barrier();

// Multiple
if (rank == 0)
    print ("Array");
data = [rank*3, rank*3+1, rank*3+2];
result = MPI.Reduce(data, MPI.Sum, 0);
if (rank == 0)
    print (result, "\n");

MPI.Barrier();

// CArray
if (rank == 0)
    print ("CArray");

data = new CArray.Integer( 3 );
data[0] = rank*3;
data[1] = rank*3+1;
data[2] = rank*3+2;

result = MPI.Reduce(data, MPI.Sum, 0);
if ( rank == 0 )
{
    for (i=0; i<result.length; i++)
        printf(result[i] + ",");
    print("\n");
}

MPI.Barrier();
*/

// Custom JS
if (rank == 0)
    print ("Custom");

if (rank == 0)
    data = [];
else
    data = File.Load("tautology.ls", "rb").split("n");
sdata = new CMatrix(data)
result = MPI.Reduce(data, custom, 0);
if (rank == 0)
    print (result)




